package seGobangClient.gaming;
import java.util.HashMap;
import seGobangClient.*;
import seGobangClient.onlineHall.OnlineHallController;

/**
 *
 * @author plus
 */
public class GamingController implements seGobangClient.ISceneController{
    private HashMap <String,ICommand> CommandsPool;
    IPlayer currentPlayer,waitingPlayer;
    ChessBoard currentChessBoard;
    OnlineHallController OnlineHall;
    IGamingScene Scene;
    private GamingCommandsFactory CommandsFactroy=new GamingCommandsFactory();
    public static int RefreshRate=1000;
    
    public GamingController(IUISystem Scene,IPlayer player1, IPlayer player2) {
        
        player1.setController(this);
        player2.setController(this);
        this.currentPlayer=player1;
        this.waitingPlayer=player2;
        
        this.Scene=(IGamingScene) Scene;
        Scene.setCurrentController(this);
        Scene.currentSceneChangeTo("Gaming");
        
        currentChessBoard=new ChessBoard(this,GlobalController.getCurrentRule());
        blendCommands();
        
        this.Scene.initPlayerInfo(currentPlayer, waitingPlayer);
         
        currentChessBoard.clearChessBoard(); 
        currentPlayer.turnToMe();
    }
    
    public GamingController(IUISystem Scene,IPlayer player1, IPlayer player2,OnlineHallController OnlineHall) {
        this.OnlineHall=OnlineHall;
        
        player1.setController(this);
        player2.setController(this);
        this.currentPlayer=player1;
        this.waitingPlayer=player2;
        
        this.Scene=(IGamingScene) Scene;
        Scene.setCurrentController(this);
        Scene.currentSceneChangeTo("Gaming");
        
        currentChessBoard=new ChessBoard(this,GlobalController.getCurrentRule());
        blendCommands();
        
        this.Scene.initPlayerInfo(currentPlayer, waitingPlayer);
         
        currentChessBoard.clearChessBoard(); 
        currentPlayer.turnToMe();
    }

    public void blendCommands() {
        CommandsPool=new HashMap <String,ICommand>();
        CommandsPool.put("ChooseChessBoard", CommandsFactroy.createChooseChessBoardCommand(this));
        CommandsPool.put("ChooseRegret", CommandsFactroy.createChooseRegretCommand(this));
        CommandsPool.put("ChooseSurrender", CommandsFactroy.createChooseSurrenderCommand(this));
        CommandsPool.put("TestPosition", CommandsFactroy.createTestPosition(this));
        CommandsPool.put("EndGame", CommandsFactroy.createEndGameCommand(this));
        
        CommandsPool.put("AddBlackChess", CommandsFactroy.createAddBlackChessCommand(this.Scene));
        CommandsPool.put("AddWhiteChess", CommandsFactroy.createAddWhiteChessCommand(this.Scene));
        CommandsPool.put("RemoveChess", CommandsFactroy.createRemoveChessCommand(this.Scene));
        CommandsPool.put("Win", CommandsFactroy.createWinCommand(this));
        CommandsPool.put("Draw", CommandsFactroy.createDrawCommand(this));
        CommandsPool.put("Forbidden", CommandsFactroy.createForbiddenCommand(this));
        
        CommandsPool.put("AIFx", CommandsFactroy.createAIFx(this.Scene));
        CommandsPool.put("AIFxWin", CommandsFactroy.createAIFxWin(this.Scene));
        CommandsPool.put("DisableLocalInput", CommandsFactroy.createDisableLocalInputCommand(this.Scene));
        CommandsPool.put("EnableLocalInput", CommandsFactroy.createEnableLocalInputCommand(this.Scene));
        CommandsPool.put("UpdatePlayerInfo", CommandsFactroy.createUpdatePlayerInfoCommand(Scene,currentPlayer,waitingPlayer));
        CommandsPool.put("MarkCurrentPlayer", CommandsFactroy.createMarkCurrentPlayerCommand(Scene));
        CommandsPool.put("BlackChessPut", CommandsFactroy.createBlackChessPutCommand(currentChessBoard,this));
        CommandsPool.put("WhiteChessPut", CommandsFactroy.createWhiteChessPutCommand(currentChessBoard,this));
        CommandsPool.put("RaiseRegret", CommandsFactroy.createRaiseRegretCommand(currentChessBoard,this));
        CommandsPool.put("RaiseSurrender", CommandsFactroy.createRaiseSurrenderCommand(this));
        CommandsPool.put("SomeoneTimeout", CommandsFactroy.createSomeoneTimeoutCommand(this));
        
        CommandsPool.put("HaltGame", CommandsFactroy.createHaltGameCommand(this));
        CommandsPool.put("ExitGame", new seGobangClient.mainMenu.CommandExitGame());
        CommandsPool.put("ReturnHall", CommandsFactroy.createReturnHallCommand(this));
        
    }

    public Object doEvent(String CommandName, Object Param) {
        Object ret=null;
        try{
            //System.out.println("Command-"+CommandName);
            ret=CommandsPool.get(CommandName).execute(Param);
            
        }catch(Exception e){
            System.err.println(CommandName+" Not Exist?");
            e.printStackTrace();
        }
        return ret;
    }
    
}
